<?php $this->load->view("include/meta"); ?>
    <title><?php echo $thread['subject'];?>-打球吧</title>

    <!-- Bootstrap core CSS -->
    <link href="<?php echo site_url();?>assets/css/bootstrap.css" rel="stylesheet">
    <link href="<?php echo site_url();?>assets/css/common.css" rel="stylesheet">
    <link href="<?php echo site_url();?>assets/css/activity.css" rel="stylesheet">
    <link href="<?php echo site_url();?>assets/css/jquery-impromptu.css" rel="stylesheet">

  </head>

  <body>

    <!-- Wrap all page content here -->
    <div id="wrap">

    <!-- header start -->
    <?php $this->load->view("include/header"); ?> 
    <!-- header end -->

    <!-- main start -->
    <div class="container" id="main">

      <div class="jumbotron">

        <h3><?php echo $thread['subject'];?></h3>
        <p><?php if($this->session->userdata('uid') == $authorInfo['uid']):?>我<?php else: ?>“<a class="author-name" href="<?php echo site_url('user').'/?uid='.$authorInfo['uid'];?>" target="_blank"><?php echo $authorInfo['username'];?></a>”<?php endif;?>想邀请<span><?php echo $thread['invite']?></span>个小伙伴<span><?php echo date('m月d日H:i',strtotime($thread['playtime']));?></span>在<span><?php echo $thread['address'];?></span><?php echo $thread['realaddress']? '（'.$thread['realaddress'].'）' : '';?><span><?php $keys = array_flip(getFid()); echo $keys[$thread['fid']]?></span></p>
        <?php if($thread['tip']) :?>
        <p>ps：<?php echo $thread['tip'];?></p>
        <?php endif;?>
        <?php if(strtotime($thread['playtime']) > time()): ?>
            <?php if(isset($joined) && $this->session->userdata('uid') && in_array($this->session->userdata('uid'),$joined)):?>
        <p><a role="button" class="btn btn-success btn-lg" href="javascript:;" id="apply" ajdata="0">取消报名</a> 
            <?php else:?>
        <p><a role="button" class="btn btn-success btn-lg" href="javascript:;" id="apply" ajdata="1">我要报名</a> 
            <?php endif;?>
        <?php endif;?>

        <?php if(isset($joined)):?>
        <span><?php echo count($joined);?>人已报名</span>
        <?php endif;?>
        </p>
      </div>

    </div>    <!-- end content -->

    <div id="mid-container" class="container">
        <div class="row">
            <div class="col-md-4 col-md-push-8">
                <div class="map_label">目的地位置</div>
                <div id="map_container">
                </div>
            </div>

            <div class="col-md-8 col-md-pull-4">
                <div class="row">
                    <div class="form-group">
                        <textarea id="message" rows="4" autocomplete="off" placeholder="嗨！快来跟兄弟们侃侃" class="form-control" name="text"></textarea>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6">
                        <button type="button" class="btn btn-default" id="addDiscuss">发表</button>
                    </div>
                </div>

                <div class="map_label"></div>

                <div id="message-list">

                    <?php if(empty($posts)): ?>
                    <div class="row">
                        <div class="col-xs-offset-2 col-md-6">
                        还没有人留言，看官快来说点啥呗
                        </div>
                    </div>

                    <?php else: ?>
                    <?php foreach($posts as $item) :?>
                        <?php if($item['authorid'] == $thread['authorid']): ?>
                    <div class="row">
                        <div class="col-xs-offset-2 col-md-7 col-lg-10"><img src="/assets/images/icons/16/<?php if($item['userInfo']['gender'] == 1):?>male<?php else:?>female<?php endif;?>.png" /> <a target="_blank" href="<?php echo site_url('user').'/?uid='.$item['authorid'];?>" class="username"><?php echo $item['userInfo']['username'];?></a> <span class="date-info"><?php echo date('Y-m-d H:i',strtotime($item['dateline']));?></span></div>
                        <div class="col-xs-offset-2 col-md-7 col-lg-10"><?php echo $item['message'] ;?></div>
                    </div>
                        <?php else: ?>
                    <div class="row">
                        <div class="col-md-7 col-lg-10"><img src="/assets/images/icons/16/<?php if($item['userInfo']['gender'] == 1):?>male<?php else:?>female<?php endif;?>.png" /> <a target="_blank" href="<?php echo site_url('user').'/?uid='.$item['authorid'];?>" class="username"><?php echo $item['userInfo']['username'];?></a> <span class="date-info"><?php echo date('Y-m-d H:i',strtotime($item['dateline']));?></span></div>
                        <div class="col-md-7 col-lg-10"><?php echo $item['message'] ;?></div>
                    </div>
                        <?php endif; ?>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </div> <!--message list -->

                <button type="button" class="btn btn-success col-xs-offset-2 col-xs-8" id="more">Success</button>

            </div>
        </div>
    </div>    <!-- end content -->

    </div>    <!-- end wrap -->

    <!-- footer start -->
    <?php $this->load->view("include/footer"); ?> 
    <!-- footer end -->

    <script type="text/javascript" src="<?php echo site_url();?>assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="<?php echo site_url();?>assets/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=<?php echo BAIDU_AK;?>"></script>
    <script type="text/javascript" src="<?php echo site_url();?>assets/js/jquery-impromptu.js"></script>
    <script>

    $(function(){
        //地图相关
        var point = new BMap.Point(<?php echo $thread['longitude'];?>,<?php echo $thread['latitude'];?>);

        var map = new BMap.Map("map_container",{enableMapClick:false});  
        map.centerAndZoom(point,14);
        map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_SMALL})); 
        map.enableDragging();

        var myIcon = new BMap.Icon("<?php echo site_url();?>assets/images/icons/32/<?php echo $thread['fid'];?>.png",new BMap.Size(32,32));
        var marker = new BMap.Marker(point,{icon:myIcon});
        map.addOverlay(marker);

        var fitMasker = $(document).height() > $(window).height() + 20 ? false:true;

        //报名
        $('#apply').click(function()
        {
            if($('#apply').attr('ajdata') == 1)
            {
                var url = 'api/activity/joinActivity';
                var m   = '报名成功';
            }
            else
            {
                var url = 'api/activity/unjoin';
                var m   = '取消成功';
            }

            $.ajax({
                url: "<?php echo site_url();?>"+url,
                type: "GET",
                dataType: "json",
                data: {tid:<?php echo $thread['tid'];?>},
                success: function(data){

                    if(data.error == 0)
                    {
                        $.prompt(m,{
                                buttons: { "确定": true },
                                fitMasker: fitMasker,
                                isButtonHide:true
                                });
                        setTimeout(function(){$.prompt.close();window.location.reload();},2000);
                    }
                    else
                    {
                        $.prompt(data.msg,{
                                buttons: { "确定": true },
                                fitMasker: fitMasker,
                                });

                        $("div").data("enable",true);

                        return false;
                    }

                }

            });

        });

        //发表评论
        $("div").data("enable",true);

        $('#addDiscuss').click(function()
        {
            if(!$("div").data("enable")) 
            {
                return false;
            }
            else
            {
                $("div").data("enable",false);
            }

            var postData = {
                            fid:<?php echo $thread['fid'];?>,
                            tid:<?php echo $thread['tid'];?>,
                            message:$('#message').val()
            }

            $.ajax({
                url: "<?php echo site_url();?>api/activity/addDiscuss",
                type: "POST",
                dataType: "json",
                data: postData,
                success: function(data){

                    if(data.error == 0)
                    {
                        $.prompt('发表成功',{
                                buttons: { "确定": true },
                                fitMasker: fitMasker,
                                isButtonHide:true
                                });
                        setTimeout(function(){$.prompt.close();window.location.reload();},2000);
                    }
                    else
                    {
                        $.prompt(data.msg,{
                                buttons: { "确定": true },
                                fitMasker: fitMasker,
                                });

                        $("div").data("enable",true);

                        return false;
                    }

                }

            });

        });
    });

    </script>
    </body>
</html>
